package descentFly.away3D4.element
{
	import away3d.containers.ObjectContainer3D;
	import away3d.entities.Mesh;
	import away3d.materials.DefaultMaterialBase;
	import away3d.tools.utils.Bounds;
	
	import descentFly.away3D4.df_awaySpace;
	import descentFly.away3D4.df_awayUtils;
	import descentFly.away3D4.df_baseSpaceElement;
	import descentFly.away3D4.element.Extends.df_controller;
	import descentFly.away3D4.material.df_UVAnimMaterial;
	import descentFly.data.df_fields;
	import descentFly.df_utils;
	
	import flash.events.Event;
	import flash.geom.Matrix3D;
	import flash.geom.Vector3D;
	
	import jiglib.geometry.JSphere;
	import jiglib.geometry.JTriangleMesh;
	import jiglib.physics.RigidBody;
	
	public class df_element extends df_baseSpaceElement {
		public static const SETMODEL: String = 'SETMODEL';
		
		private var _model		: ObjectContainer3D;
		private var _controller	: df_controller;
		private var _bodys		: Array;
		
		public function df_element(mat: Matrix3D, a_space:df_awaySpace, a_options:Object) {
			super(a_space, a_options);
			modelInit(mat);
		}
		
		protected function modelInit(mat: Matrix3D): void {
			_bodys = new Array();
		}
		
		protected function set model(a_model: ObjectContainer3D): void {
			setModel(a_model);
		}
		
		public function parse_controller(params: Object): void {
			_controller = new (params.Class)(this, params.options);		
		}
		
		protected function setModel(a_model: ObjectContainer3D): void {
			if (_model) _model.dispose(false);
			_model = a_model;
			if (_model) {
				df_awayUtils.applyMaterialOptionsFromModel(_model, options, addModelFromScene);
			} else dispatchEvent(new Event(SETMODEL));
		}
		
		protected function createBodys(names: Array): void {
			names.forEach(function(name: String, i: int, arr: Array): void {
				var mesh: Mesh = df_awayUtils.objectByName(_model, name) as Mesh;
				if (mesh) {
					var mat: Matrix3D 	= mesh.sceneTransform;
					var p: Object = df_utils.matToObject(mat);
					_model.parent.addChild(mesh);
//					Space.physic.createSphere(
					var body: RigidBody = Space.physic.createMesh(mesh, p.position, p.orientation);
					body.movable = false;
					_bodys.push([body, mesh]);
				}
			});
		}
		
		private function addModelFromScene(): void {
			Space.view.view.scene.addChild(_model);
			if (options.bodys) createBodys(options.bodys.split(','));
			
			dispatchEvent(new Event(SETMODEL));
		}
		
		public function getModel(): ObjectContainer3D {
			return _model;
		}
		
		public function pickUp(): void {
			_controller.pickUp();
			dispose();
		}
		
		protected function removeBodys(): void {
			if (_bodys) _bodys.forEach(function(body: Array, i: int, arr: Array): void {
				Space.physic.removeBody(body[0]);
				body[1].dispose(true);
			});
		}
		
		override public function dispose():void {
			if (_controller) _controller.dispose();
			if (_model) _model.dispose(false);
			removeBodys();
			super.dispose();
		}
	}
}